package com.quickboot.system.service.impl;

import cn.hutool.core.comparator.CompareUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quickboot.common.consts.MsgConst;
import com.quickboot.common.entity.Result;
import com.quickboot.common.utils.AssertUtils;
import com.quickboot.system.entity.SysRole;
import com.quickboot.system.entity.SysRolePermission;
import com.quickboot.system.mapper.SysRoleMapper;
import com.quickboot.system.service.SysRolePermissionService;
import com.quickboot.system.service.SysRoleService;
import com.quickboot.system.vo.SysRoleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 角色 接口实现类
 *
 * @author Tellsea
 * @date 2022-12-17
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRolePermissionService rolePermissionService;

    @Override
    public Page<SysRoleVo> queryList(Page page, SysRoleVo entity) {
        return this.baseMapper.queryList(page, entity);
    }

    @Override
    public SysRoleVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public Result createRole(SysRoleVo entity) {
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleKey, entity.getRoleKey()));
        AssertUtils.isTrue(CompareUtil.compare(count, 0L) > 0, "角色标识已存在");
        this.baseMapper.insert(entity);
        handlePermission(entity);
        return Result.ok(MsgConst.SAVE_SUCCESS);
    }

    @Override
    public Result updateRole(SysRoleVo entity) {
        List<SysRole> roleList = this.baseMapper.selectList(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleKey, entity.getRoleKey()));
        if (!CollectionUtils.isEmpty(roleList)) {
            AssertUtils.isTrue(roleList.size() > 1, "存在多个异常角色标识");
            AssertUtils.isTrue(roleList.size() == 1 && CompareUtil.compare(roleList.get(0).getId(), entity.getId()) != 0, "角色标识已经被使用");
        }
        this.baseMapper.updateById(entity);
        rolePermissionService.remove(new LambdaQueryWrapper<SysRolePermission>().eq(SysRolePermission::getRoleId, entity.getId()));
        handlePermission(entity);
        return Result.ok(MsgConst.UPDATE_SUCCESS);
    }

    /**
     * 新增/更新权限公共方法
     *
     * @param entity
     */
    private void handlePermission(SysRoleVo entity) {
        if (!Objects.isNull(entity.getPermissionIds())) {
            List<SysRolePermission> permissionList = new ArrayList<>();
            String[] permissionIds = entity.getPermissionIds().split(",");
            for (int i = 0; i < permissionIds.length; i++) {
                permissionList.add(new SysRolePermission().setRoleId(entity.getId()).setPermissionId(Long.valueOf(permissionIds[i])));
            }
            rolePermissionService.saveBatch(permissionList);
        }
    }
}
